package com.polarbear.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {
	public static Date getFirstDay(int year, int month){
		Calendar cal = Calendar.getInstance();
		
		cal.set(year, month - 1, 1);
		
		cal.add(Calendar.DAY_OF_MONTH, -(cal.get(Calendar.DAY_OF_WEEK) - 1));
		return cal.getTime();
	}
	
	public static Date getLastDay(int year, int month){
		Calendar cal = Calendar.getInstance();
		
		if(month == 12){
			cal.set(year + 1, 0, 1);
		} else {
			cal.set(year, month, 1);
		}
		
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.add(Calendar.DAY_OF_MONTH, (7-cal.get(Calendar.DAY_OF_WEEK)));
		
		return cal.getTime();
	}
	
	public static String getFirstDayString(int year, int month){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		cal.set(year, month - 1, 1);
		
		cal.add(Calendar.DAY_OF_MONTH, -(cal.get(Calendar.DAY_OF_WEEK) - 1));
		return sdf.format(cal.getTime());
	}
	
	public static String getLastDayString(int year, int month){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		if(month == 12){
			cal.set(year + 1, 0, 1);
		} else {
			cal.set(year, month, 1);
		}
		
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.add(Calendar.DAY_OF_MONTH, (7-cal.get(Calendar.DAY_OF_WEEK)));
		
		return sdf.format(cal.getTime());
	}

	public static java.util.Date stringToDate(String str){
		java.util.Date rtn = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		
		try {
			rtn = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return rtn; 
	}
	
	public static java.sql.Date stringToSqlDate(String str){
		java.util.Date date = stringToDate(str);
		java.sql.Date rtn = new java.sql.Date(date.getTime());
		return rtn;
	}
	
	public static String dateToString(java.util.Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		
		return sdf.format(date);
	}
	
	public static String sqlDateToString(java.sql.Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		
		//date.getTime()
		
		return sdf.format(date);
	}
	
	public static java.util.Date sqlDateToDate(java.sql.Date date){
		return new java.util.Date(date.getTime());
	}
	
	public static java.sql.Date dateToSqlDate(java.util.Date date){
		return new java.sql.Date(date.getTime());
	}
	
	public static int stringToGetDay(String date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(stringToDate(date));
		return cal.get(Calendar.DATE);
	}
	
	public static int stringToGetMonth(String date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(stringToDate(date));
		return cal.get(Calendar.MONTH) + 1;
	}
	public static int stringToGetYear(String date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(stringToDate(date));
		return cal.get(Calendar.YEAR);
	}
	
	public static int getDateDiff(String date1, String date2){
		Date d1 = stringToDate(date1);
		Date d2 = stringToDate(date2);
		return getDateDiff(d1, d2);
	}
	public static int getDateDiff(String date1, Date date2){
		Date d1 = stringToDate(date1);
		return getDateDiff(d1, date2);
	}
	public static int getDateDiff(Date date1, String date2){
		Date d2 = stringToDate(date2);
		return getDateDiff(date1, d2);
	}
	public static int getDateDiff(Date date1, Date date2){
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		
		if(cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
				&& (cal1.get(Calendar.MONTH) + 1) == (cal2.get(Calendar.MONTH) + 1)){
			return cal1.get(Calendar.DATE) - cal2.get(Calendar.DATE);
		}else if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
				&& (cal1.get(Calendar.MONTH) + 1) == (cal2.get(Calendar.MONTH) + 1)
				&& cal1.get(Calendar.DATE) == cal2.get(Calendar.DATE)){
			return 0;
		}else{
			Calendar t1 = Calendar.getInstance();
			Calendar t2 = Calendar.getInstance();
			t1.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH) + 1, cal1.get(Calendar.DATE));
			t2.set(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH) + 1, cal2.get(Calendar.DATE));
			
			return (int)Math.ceil((double)(t1.getTimeInMillis() - t2.getTimeInMillis())/(60*60*24*1000)) - 1;
		}
		//return ((date1.getTime() - date2.getTime())<60*60*24*1000)?0:(int)Math.floor((double)(date1.getTime() - date2.getTime())/(60*60*24*1000));
	}
}
